package com.qf.web;

import com.qf.pojo.Emp;
import com.qf.service.EmpService;
import com.qf.service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/addEmp")
public class addEmpServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id=Integer.parseInt(req.getParameter("id"));
        String ename=req.getParameter("ename");
        int job_id=Integer.parseInt(req.getParameter("job_id"));
        int mgr=Integer.parseInt(req.getParameter("mgr"));

        //获取当前时间并转换成 java.sql.Date类型
        //
        String joindate=req.getParameter("joindate");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date parse=null;
        try {
             parse = sdf.parse(joindate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        java.sql.Date joindate1 = new java.sql.Date(parse.getTime());
        String salary=req.getParameter("salary");
        String bonus=req.getParameter("bonus");
        int dept_id=Integer.parseInt(req.getParameter("dept_id"));

        Emp emp=new Emp(id,ename,job_id,mgr,joindate1,salary,bonus,dept_id);

        EmpService empService=new EmpServiceImpl();
        int num = empService.addEmp(emp);
        PrintWriter writer = resp.getWriter();
        if(num==1){
            writer.println("添加成功");
        }else {
            writer.println("添加失败");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
